package com.xnfh.home.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.framework.bean.TidingBean
import com.xnfh.home.R
import com.xnfh.home.adapter.MessageAdapter
import com.xnfh.home.databinding.ActivityMessageLayoutBinding
import com.xnfh.home.viewmodel.MessageViewModel

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2024/1/4 13:33
 * @UpdateUser: ziheng
 * @UpdateDate: 2024/1/4 13:33
 * @UpdateRemark: 更新说明
 */
class MessageActivity: BaseActivity<ActivityMessageLayoutBinding>() {
    override fun getLayoutID(): Int = R.layout.activity_message_layout
    companion object {
        //type 1消息，2反馈，3俱乐部
        fun start(context: Context, bundle: Bundle? = null) {
            val intent = Intent(context, MessageActivity::class.java)
            if (bundle != null) {
                intent.putExtra("bundle", bundle)
            }
            context.startActivity(intent)
        }
    }
    private lateinit var adapter: MessageAdapter
    private lateinit var viewModel: MessageViewModel

    override fun init() {
        val bundle = intent.getBundleExtra("bundle")
        val type = bundle?.getInt("type", 1)
        //设置title
        when(type){
            1 ->{
                setHeaderTitle(getString(R.string.str_message))
            }
            2->{
                setHeaderTitle(getString(R.string.str_feedback))
            }
            3->{
                setHeaderTitle(getString(R.string.str_club))
            }
        }
        mBind.messageRecycle.layoutManager = GridLayoutManager(this,2)
        adapter = MessageAdapter()
        mBind.messageRecycle.adapter = adapter
        viewModel.requestMessageOrOther(type = type!!)
        adapter.setOnItemClickListener { adapter, view, position ->
            val bundle1 = Bundle()
            when (type) {
                1 -> {
                    bundle1.putInt("id", (adapter.data[position] as TidingBean).id)
                    bundle1.putString("name", (adapter.data[position] as TidingBean).typeName)
                    MessageDetailActivity.start(this,bundle1)
                    (adapter.data[position] as TidingBean).read = 0
                    adapter.notifyItemChanged(position)
                }
                2 -> {
                    bundle1.putInt("id", (adapter.data[position] as TidingBean).id)
                    FeedBackActivity.start(this,bundle1)
                }
                3 -> {
                    bundle1.putString("cropId",(adapter.data[position] as TidingBean).cropIds)
                    bundle1.putString("name",(adapter.data[position] as TidingBean).typeName)
                    ClubDetailActivity.start(this,bundle1)
                }
            }
        }
    }

    override fun observe() {
        viewModel = MessageViewModel()
        viewModel.tidingBeanS.observe(this,object :BaseStateObserver<List<TidingBean>>(){
            override fun getRespDataSuccess(it: List<TidingBean>) {
                super.getRespDataSuccess(it)
                dismissLoadingDialog()
                adapter.setNewInstance(it.toMutableList())
            }

            override fun getRespDataStart() {
                super.getRespDataStart()
                showLoadingDialog()
            }

            override fun getRespDataEnd() {
                super.getRespDataEnd()
                dismissLoadingDialog()
            }
        })
    }
}